package org.streets.eis.component.grid;


import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;

import com.inmethod.grid.column.AbstractColumn;
import org.streets.eis.component.ChangedNotifyType;

public class BooleanColumn extends EditableColumn {

	private static final long serialVersionUID = 1L;

	private ChangedNotifyType changedNotifyType = ChangedNotifyType.NONE;
	
	@SuppressWarnings("unchecked")	
	public BooleanColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty) {
		super(columnId, headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public BooleanColumn(String columnId, IModel headerModel, String propertyExpression) {
		super(columnId, headerModel, propertyExpression);
	}

	@SuppressWarnings("unchecked")	
	public BooleanColumn(IModel headerModel, String propertyExpression, String sortProperty) {
		super(headerModel, propertyExpression, sortProperty);
	}

	@SuppressWarnings("unchecked")	
	public BooleanColumn(IModel headerModel, String propertyExpression) {
		super(headerModel, propertyExpression);
	}
	
	@SuppressWarnings("unchecked")
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		if (getChangedNotifyType()==ChangedNotifyType.AJAX){
			return new AjaxCheckBox(id,model){			
				private static final long serialVersionUID = 1L;

				@Override
				protected void onComponentTag(final ComponentTag tag){
					tag.put("type", "checkbox");
					super.onComponentTag(tag);
					tag.remove("onkeyup");
					tag.remove("onfocus");
					tag.remove("onkeypress");					
				}
			
				@Override
				protected void onUpdate(AjaxRequestTarget target) {
					BooleanColumn.this.onAjaxSelectionChanged(this,this.getModelObject(),target);				
				}
			};
		} else {
			return new CheckBox(id,model){			
				private static final long serialVersionUID = 1L;

				@Override
				protected void onComponentTag(final ComponentTag tag){
					tag.put("type", "checkbox");
					super.onComponentTag(tag);
					tag.remove("onkeyup");
					tag.remove("onfocus");
					tag.remove("onkeypress");
				}
				
				@Override
				protected boolean wantOnSelectionChangedNotifications(){
					return BooleanColumn.this.changedNotifyType == ChangedNotifyType.FORM;
				}
				
				@Override
				protected void onSelectionChanged(Object newSelection){
					BooleanColumn.this.onSelectionChanged(this,(Boolean)newSelection);
				}
			};
		}
	}
	
	public ChangedNotifyType getChangedNotifyType() {
		return changedNotifyType;
	}

	public void setChangedNotifyType(ChangedNotifyType changedNotifyType) {
		this.changedNotifyType = changedNotifyType;
	}	
	
	protected void onSelectionChanged(CheckBox component,Boolean checked){		
	}
	
	protected void onAjaxSelectionChanged(CheckBox component,Boolean checked,AjaxRequestTarget target) {		
	}
	
}
